<?php
/**
 * 用户打卡签到类
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/20 0020
 * Time: 下午 12:25
 */
namespace App\Http\Controllers\Backstage;

use App\Http\Controllers\Controller;
use App\Model\Backstage\Staffattendance;
use App\Tool\Error;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class SigninController extends Controller
{
    /**上班打卡
     * @param Request $request
     */
    public function goToWork(Staffattendance $staffattendance)
    {
        $dayTime = Carbon::today()->toDateString();
        $res = $staffattendance->gotoWork($dayTime);
        if ($res['code'] == 200){
            return $this->success('ok', '上班打卡成功');
        }else{
            return $this->error('400', $res['msg']);
        }
    }

    /** 下班打卡
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function afterWork(Request $request, Staffattendance $staffattendance)
    {
        $dayTime = Carbon::today()->toDateString();
        $res = $staffattendance->afterWork($dayTime,$request);
        if ($res['code'] == 200){
            return $this->success('ok', '下班打卡成功');
        }else{
            return $this->error('400', $res['msg']);
        }
    }
    /**
     * 签到排行榜
     */
    public function Rankings(Request $request)
    {
        $map = [];
        // 判断有没有统计开始时间和结束时间  如果有按时间来查询  如果没有 查询当前月份
        if($request->has('start_time')){
            $map[] = ['date', '>=', $request->input('start_time')];
        }
        if($request->has('end_time')){
            $map[] = ['date', '<=', $request->input('end_time')];
        }
        if(empty($map)){
            $map[] = ['date', '>=',  date('Y-m-01', strtotime(date("Y-m-d")))];
            $map[] = ['date', '<=', date('Y-m-d')];
        }
        //按签到类型查找
        if($request->has('sign_type') && $request->input('sign_type') !=''){
            $map[] = ['sign_type', $request->input('sign_type')];
        }
        //按部门查找
        if($request->has('s_d_id')){
            $map[] = ['s_d_id', $request->input('s_d_id')];
        }
        $classStaff = new Staffattendance();
        $res = $classStaff->staffRankings($map, env('PAGE_LIMIT_ADMIN'));
        if (sizeof($res) < 1){
            return $this->error(Error::NULL_DATA);
        }
        return $this->success('ok', $res);
    }

    /**
     * @param Request $request
     * @param Staffattendance $staffattendance
     * @return \Illuminate\Http\JsonResponse
     * 部门经理针对员工签到点评
     */
    public function siginComment(Request $request, Staffattendance $staffattendance){
        if(!$request->has('comments') || !$request->has('id')){
            return $this->error(Error::PARAM_MISSING);
        }
        $res = $staffattendance->siginComment($request->input('id'), $request->input('comments'));
        if($res){
            return $this->success('ok','点评成功');
        }
        return $this->error(Error::ADD_ERROR);

    }
}